package com.mvp.lt.airlineview.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.Log;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;

import com.mvp.lt.airlineview.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.util.Util;

import java.io.File;


/**
 * $activityName
 *
 * @author ${LiuTao}
 * @date 2018/5/15/015
 */

public class GlideLoadUtils {
    private String TAG = "ImageLoader";

    /**
     * 借助内部类 实现线程安全的单例模式
     * 属于懒汉式单例，因为Java机制规定，内部类SingletonHolder只有在getInstance()
     * 方法第一次调用的时候才会被加载（实现了lazy），而且其加载过程是线程安全的。
     * 内部类加载的时候实例化一次instance。
     */
    public GlideLoadUtils() {
    }

    private static class GlideLoadUtilsHolder {
        private final static GlideLoadUtils INSTANCE = new GlideLoadUtils();
    }

    public static GlideLoadUtils getInstance() {
        return GlideLoadUtilsHolder.INSTANCE;
    }

    /**
     * Glide 加载 简单判空封装 防止异步加载数据时调用Glide 抛出异常
     *
     * @param context
     * @param url           加载图片的url地址  String
     * @param imageView     加载图片的ImageView 控件
     * @param default_image 图片展示错误的本地图片 id
     */
    public void glideLoad(Context context, String url, ImageView imageView, int default_image) {
        try {
            if (context != null) {
                RequestOptions requestOptions = new RequestOptions()
                        .centerCrop()
                        .placeholder(R.color.translucent)
                        .priority(Priority.HIGH)
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .error(default_image);
                Glide.with(context.getApplicationContext())
                        .load(url)
                        .apply(requestOptions).into(imageView);
            } else {
                Log.i(TAG, "Picture loading failed,context is null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void glideLoad(Context context, String url, ImageView imageView, int default_image, RequestOptions requestOptions) {
        try {
            if (context != null) {
                Glide.with(context.getApplicationContext()).load(url).apply(requestOptions).into(imageView);
            } else {
                Log.i(TAG, "Picture loading failed,context is null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void glideLoad(Context context, File url, ImageView imageView, int default_image) {
        try {
            if (context != null) {
                RequestOptions requestOptions = new RequestOptions().centerCrop().placeholder(R.color.translucent)
                        .error(R.drawable.hed);
                Glide.with(context.getApplicationContext()).load(url).apply(requestOptions).into(imageView);
            } else {
                Log.i(TAG, "Picture loading failed,context is null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void glideLoad(Context context, byte[] bytes, ImageView imageView, int default_image, Drawable drawable) {
        try {
            if (context != null) {
                RequestOptions requestOptions = new RequestOptions().centerCrop().error(default_image);
                Glide.with(context.getApplicationContext()).load(bytes).apply(requestOptions).into(imageView);
            } else {
                Log.i(TAG, "Picture loading failed,context is null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public void glideLoad(Context context, byte[] bytes, ImageView imageView, int default_image) {
        try {
            if (Util.isOnMainThread()) {
                RequestOptions requestOptions = new RequestOptions().centerCrop().placeholder(R.color.translucent)
                        .error(R.drawable.hed);
                Glide.with(context.getApplicationContext()).load(bytes).apply(requestOptions).into(imageView);
            } else {
                Log.i(TAG, "Picture loading failed,context is null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public void glideLoad(Activity activity, String url, ImageView imageView, int default_image) {
        try {
            if (!activity.isDestroyed()) {
                RequestOptions requestOptions = new RequestOptions()
                        .centerCrop().placeholder(R.color.translucent)
                        .error(R.drawable.hed);
                if (url == null) {
                    url = " ";
                }
                Glide.with(activity.getApplicationContext()).load(url).apply(requestOptions).into(imageView);
            } else {
                Log.i(TAG, "Picture loading failed,activity is Destroyed");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void glideLoad(Fragment fragment, String url, ImageView imageView, int default_image) {
        try {
            if (fragment != null && fragment.getActivity() != null) {
                RequestOptions requestOptions = new RequestOptions().centerCrop().error(default_image);
                Glide.with(fragment.getActivity().getApplicationContext()).load(url).apply(requestOptions).into(imageView);
            } else {
                Log.i(TAG, "Picture loading failed,fragment is null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void glideLoad(android.app.Fragment fragment, String url, ImageView imageView, int default_image) {
        try {
            if (fragment != null && fragment.getActivity() != null) {
                RequestOptions requestOptions = new RequestOptions().centerCrop().error(default_image);
                Glide.with(fragment.getActivity().getApplicationContext()).load(url).apply(requestOptions).into(imageView);
            } else {
                Log.i(TAG, "Picture loading failed,android.app.Fragment is null");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
